package com.xiaoyang.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lichenyang
 * @create 2021-07-15 14:48
 *
 * 每访问一次页面, redis 就计数一次
 */
@Component
public class RedisUrlInterceptor  implements HandlerInterceptor {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();

        // 每访问一次 uri 就+1
        redisTemplate.opsForValue().increment(requestURI);
        return true;
    }
}
